﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Data.OleDb

Public Class DB_Delegate
    Public Table As New DataTable
    Public SQLConnStr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TYCSDDB;Data Source=localhost"
    Public SYBASEStr As String = "DSN=oss;UID=sa;PWD=sybase11;" 'Sybase库的ODBC数据源

    Public Function FillDataTable(ByVal selectCommand As String, Optional ByVal tablename As String = "", Optional ByVal isSybaseConStr As Boolean = False) As String
        '函数说明：1、若isSybaseConStr  = False表示连的是本地SQL数据库，否则连OSS的Sybase数据库
        '          2、第二个参数为datatable的名称，此方法运行后通过将Table赋给datatable对象，次名称也将赋给该datatable
        Try
            ' 依据特定的select语句创建一个新的 data adapter。
            'Dim dataAdapter As SqlDataAdapter
            'Dim dataAdapterSyb As OdbcDataAdapter
            If isSybaseConStr = False Then
                '连本地SQLSERVER数据库
                Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(selectCommand, SQLConnStr)
                Dim commandBuilder As New SqlCommandBuilder(dataAdapter)
                Table.Locale = System.Globalization.CultureInfo.InvariantCulture
                dataAdapter.Fill(Table)
            Else
                '连远程sybase库
                Dim dataAdapter As OdbcDataAdapter = New OdbcDataAdapter(selectCommand, SYBASEStr)
                Try
                    dataAdapter.Fill(Table)
                    If tablename <> "" Then Table.TableName = tablename
                Catch ex As Exception
                    Return ex.ToString()
                End Try

            End If


            ' Create a command builder to generate SQL update, insert, and
            ' delete commands based on selectCommand. These are used to
            ' update the database.


            ' Populate a new data table and bind it to the BindingSource.
            'Dim Table As New DataTable()


            Return ""
            'Me.BindingSource1.DataSource = table
            ' Resize the DataGridView columns to fit the newly loaded content.
            'FormX.DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
        Catch ex As SqlException
            Return "数据库查询失败！请确认数据库运行正常,且所查询时间正确！错误代码：" & ex.ToString()
        End Try

    End Function


End Class
